/**
 * 系统平台
 * moonvip_admin
 * com.tz.web.role
 * RoleController.java
 * 创建人:小胡
 * 时间：2016年11月26日 14:39:13
 *
 */
package com.tz.web.role;

import com.tz.auth.AuthPassport;
import com.tz.bean.Role;
import com.tz.bean.TzParams;
import com.tz.service.adminUser.IAdminUserService;
import com.tz.service.role.IRoleService;
import com.tz.util.TmStringUtils;
import org.apache.struts2.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 角色管理web
 * RoleController
 * 创建人:小胡
 * 时间：2016年11月26日 14:39:13
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController {
	@Autowired
	private IRoleService roleService;

	@Autowired
	private IAdminUserService adminUserService;


	/**
	 * 描述: 方法描述
	 * 包名： com.tz.web.role
	 * 类名：RoleController
	 * 方法名: list
	 * 参数类型: [params]
	 * 创建人：hufanglei
	 * 时间：2016/3/18   12:00
	 * qq：690328661
	 *
	 * @return java.lang.String
	 */
	@AuthPassport
	@RequestMapping("/list")
	public String list(TzParams params) {
		return "role/list";


	}


	/**
	 * 描述: 查询所有用户
	 * 包名： com.tz.web.role
	 * 类名：RoleController
	 * 方法名: user
	 * 参数类型: [params]
	 * 创建人：hufanglei
	 * 时间：2016/3/18   12:50
	 * qq：690328661
	 *
	 * @return org.springframework.web.servlet.ModelAndView
	 */
	@AuthPassport
	@RequestMapping("/user/{rid}")
	public ModelAndView user(TzParams params, @PathVariable("rid") Integer roleId) {
		ModelAndView modelAndView = new ModelAndView();
		params.setDeleteCode("001");
		params.setPageSize(30);
		params.setRoleId(roleId);
		List<HashMap<String, Object>> maps = roleService.findFilterUsers(params);
		int totalcount = roleService.countFilterUsers(params);
		modelAndView.addObject("datas", maps);
		modelAndView.addObject("count", totalcount);
		modelAndView.setViewName("role/user");
		return modelAndView;
	}

	@AuthPassport
	@RequestMapping("/permission/{rid}")
	public ModelAndView permission(TzParams params, @PathVariable("rid") Integer roleId) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("role/permission");
		return modelAndView;
	}


	/*模板页面*/
	@RequestMapping("/template")
	public ModelAndView template(TzParams params) {
		ModelAndView modelAndView = new ModelAndView();
		List<Role> roles = roleService.findRoles(params);
		int count = roleService.count(params);
		modelAndView.setViewName("role/template");
		modelAndView.addObject("datas", roles);
		modelAndView.addObject("itemCount", count);
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping(value = "/saveUserRole", method = RequestMethod.POST)
	public String saveUserRole(HttpServletRequest request) {
		//获取所有的用户
		String userStirng = request.getParameter("users");
		//角色id
		String rid = request.getParameter("rid");
		if (TmStringUtils.isNotEmpty(userStirng)) {
			//分配角色给用户
			String[] users = userStirng.split(",");
			for (String string : users) {
				roleService.saveRoleUser(Integer.parseInt(string), Integer.parseInt(rid));
			}
			return "success";
		} else {
			return "fail";

		}
	}


	/**
	 * 描述: 角色绑定权限
	 * 包名： com.tz.web.role
	 * 类名：RoleController
	 * 方法名: saveRolePermission
	 * 参数类型: [request]
	 * 创建人：hufanglei
	 * 时间：2016/3/19   8:50
	 * qq：690328661
	 *
	 * @return java.lang.String
	 */
	@ResponseBody
	@RequestMapping(value = "/saveRolePermission", method = RequestMethod.POST)
	public String saveRolePermission(HttpServletRequest request) {
		//获取所有的权限
		String userStirng = request.getParameter("permissions");
		//获取分配的角色
		String roleId = request.getParameter("rid");
		//如果权限有，角色不为空
		if (TmStringUtils.isNotEmpty(userStirng) && TmStringUtils.isNotEmpty(roleId)) {
			Integer rid = Integer.parseInt(roleId);
			//删除角色对应的权限
			roleService.deleteRolePermission(rid);
			//重新分配权限
			String[] users = userStirng.split(",");
			for (String string : users) {
				roleService.saveRolePermission(rid, Integer.parseInt(string));
			}
			return "success";
		} else {
			return "fail";

		}
	}


	@ResponseBody
	@RequestMapping(value = "/findPermissionByRoleId", method = RequestMethod.POST)
	public Integer[] findPermissionByRoleId(HttpServletRequest requset) throws JSONException {
		String roleId = requset.getParameter("roleId");
		if (TmStringUtils.isNotEmpty(roleId)) {
			Integer[] permissionIds = roleService.findPermissionByRoleId(Integer.parseInt(roleId));
			return permissionIds;
		} else {
			return null;
		}
	}


	@ResponseBody
	@RequestMapping(value="/update",method= RequestMethod.POST)
	public String update(Role role){
		roleService.update(role);
		return "success";
	}

}
